﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Bussiness;

namespace GUI
{
    public partial class frmPhieuNhapSach : Form
    {
        private DataTable _sourceTable = new DataTable();
        private int _minEntry;
        private int _minInStock;

        public frmPhieuNhapSach()
        {
            InitializeComponent();
        }

        private void frmPhieuNhapSach_Load(object sender, EventArgs e)
        {
            idSachColumn.Visible = false;
            btnLuu.Enabled = true;

            SachService sService = new SachService();

            this._sourceTable = sService.SelectSachDetail();            

            maSachComlumn.DataSource = this._sourceTable;
            maSachComlumn.DisplayMember = SachComlumn.MaSach;
            maSachComlumn.ValueMember = SachComlumn.IDSach;
            
            //((ComboBox)maSachComlumn).AutoCompleteMode = AutoCompleteMode.SuggestAppend;
           // maSachComlumn.Disposed += new System.EventHandler(this.test);
            PhieuNhapService pnService = new PhieuNhapService();
            tbxMaPhieuNhap.Text = pnService.CreateMaPhieuNhap();

            //quy dinh nhap
            ThamSoService ts = new ThamSoService();
            _minEntry = ts.GetRules(ThamSoRows.SoLuongNhap);
            _minInStock = ts.GetRules(ThamSoRows.SoLuongTon);
            lbNhapToiThieu.Text = lbNhapToiThieu.Text + _minEntry;
            lbTonToiThieu.Text = lbTonToiThieu.Text + _minInStock;
            tbxTongSoSach.Text = "0";
        }

        private void btnLuu_Click(object sender, EventArgs e) {
            //kiem tra co sach nao duoc nhap hay ko
            if (dgvDanhSachNhap.Rows.Count == 1) {
                MessageBox.Show("hãy chọn sách muốn nhập", "lỗi", MessageBoxButtons.OK);
                return;
            }

            int tongSoLuong = 0;
            //kiem tra dieu kien co thoa hay ko
            for (int i = 0; i < dgvDanhSachNhap.Rows.Count - 1; i++) {
                if(dgvDanhSachNhap.Rows[i].Cells["tenSachColumn"].Value == null)
                    continue;

                int soLuongNhap = Convert.ToInt32(dgvDanhSachNhap.Rows[i].Cells["soLuongColumn"].Value);
                int soLuongTon = Convert.ToInt32(dgvDanhSachNhap.Rows[i].Cells["soLuongTonColumn"].Value);
                if (soLuongNhap < _minEntry) {
                    MessageBox.Show("Số lượng nhập tối thiểu là: " + _minEntry.ToString(), "Lỗi");
                    dgvDanhSachNhap.CurrentCell = dgvDanhSachNhap.Rows[i].Cells["soLuongColumn"];
                    return;
                }
                if (soLuongTon > _minInStock) {
                    MessageBox.Show("Số lượng tồn phải ít hơn: " + _minInStock.ToString(), "Lỗi");
                    dgvDanhSachNhap.CurrentCell = dgvDanhSachNhap.Rows[i].Cells["soLuongColumn"];
                    return;
                }

                tongSoLuong += soLuongNhap;
            }

            //luu phieu nhap va danhs ach chi tiet
            PhieuNhapDTO phieuDTO = new PhieuNhapDTO();
            phieuDTO.MaPhieuNhap = tbxMaPhieuNhap.Text;
            phieuDTO.NgayNhap = dtpNgayNhapSach.Value;
            phieuDTO.TongSoLuong = tongSoLuong;

            List<ChiTietPhieuNhapDTO> chiTietList = new List<ChiTietPhieuNhapDTO>();
            for (int i = 0; i < dgvDanhSachNhap.Rows.Count - 1; i++ ) {
                DataGridViewRow row = dgvDanhSachNhap.Rows[i];
                ChiTietPhieuNhapDTO chiTiet = new ChiTietPhieuNhapDTO();
                chiTiet.IDSach = Convert.ToInt32(row.Cells["idSachColumn"].Value);
                chiTiet.SoLuong = Convert.ToInt32(row.Cells["soLuongColumn"].Value);

                chiTietList.Add(chiTiet);
            }
            
            PhieuNhapService service = new PhieuNhapService();
            service.AddData(phieuDTO, chiTietList);

            dgvDanhSachNhap.Rows.Clear();
            btnLuu.Enabled = false;

            MessageBox.Show("xong");
        }      
        

        private void btnThoat_Click(object sender, EventArgs e) {
            Close();
        }

        #region phan hien thi thong tin
        private void dgvDanhSachNhap_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
            //chọn mã sách
            if ( dgvDanhSachNhap.CurrentCell.ColumnIndex == 1) {
                ComboBox comboBox = e.Control as ComboBox;
                comboBox.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectIndexChanged);
                comboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
                comboBox.DropDownStyle = ComboBoxStyle.DropDown;
            }

            //neu la so luong thi chi cho nhap so
            if (dgvDanhSachNhap.CurrentCell.ColumnIndex == 5) {
                e.Control.KeyPress += new KeyPressEventHandler(this.textBoxNumberic_KeyPress);
            }
        }
        private void textBoxNumberic_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
            if (!char.IsNumber(e.KeyChar))
                e.Handled = true;
        }

        private void comboBox_SelectIndexChanged(object sender, EventArgs e) {
            int index = ((ComboBox)sender).SelectedIndex;
            if (index != -1) {
                //thiếu kiểm tra mỗi sách chỉ ở 1 dòng

               // DataTable table = (DataTable)maSachComlumn.DataSource;
                dgvDanhSachNhap.CurrentRow.Cells["tenSachColumn"].Value = _sourceTable.Rows[index][SachComlumn.TenSach];
                dgvDanhSachNhap.CurrentRow.Cells["soLuongTonColumn"].Value = _sourceTable.Rows[index][SachComlumn.SoLuongTon];
                //dgvDanhSachNhap.CurrentRow.Cells["donGiaColumn"].Value = table.Rows[index][SachComlumn.DonGiaSach];
                dgvDanhSachNhap.CurrentRow.Cells["tacGiaColumn"].Value = _sourceTable.Rows[index][TacGiaColumn.TenTacGia];
                dgvDanhSachNhap.CurrentRow.Cells["theLoaiColumn"].Value = _sourceTable.Rows[index][TheLoaiColumn.TenTheLoai];
                dgvDanhSachNhap.CurrentRow.Cells["idSachColumn"].Value = _sourceTable.Rows[index][SachComlumn.IDSach];
                dgvDanhSachNhap.CurrentRow.Cells["soLuongcolumn"].Value = _minEntry;
                //dgvDanhSachNhap.CurrentRow.Cells["sttColumn"].Value = dgvDanhSachNhap.CurrentRow.Index;

                //dgvDanhSachNhap.CurrentRow.Cells["soLuongcolumn"].ReadOnly = false;
            }
        }

        private void dgvDanhSachNhap_CellEndEdit(object sender, DataGridViewCellEventArgs e) {
            //kiem tra neu nhap lieu ma chua chon sach
            


            int tongSoLuongNhap = 0;
            for (int i = 0; i < dgvDanhSachNhap.Rows.Count; i++) {
                dgvDanhSachNhap.Rows[i].Cells["sttColumn"].Value = (i + 1).ToString();
                int soLuongNhap = Convert.ToInt32(dgvDanhSachNhap.Rows[i].Cells["soLuongColumn"].Value);
                tongSoLuongNhap += soLuongNhap;
            }
            tbxTongSoSach.Text = tongSoLuongNhap.ToString();

         }
        #endregion

        private void dgvDanhSachNhap_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) {
            //if (dgvDanhSachNhap.Rows[e.RowIndex].IsNewRow)
            //    return;

            //if (e.ColumnIndex == 5) {
            //    int soLuongNhap = Convert.ToInt32(dgvDanhSachNhap.Rows[e.RowIndex].Cells["soLuongColumn"].Value);
            //    int soLuongTon = Convert.ToInt32(dgvDanhSachNhap.Rows[e.RowIndex].Cells["soLuongTonColumn"].Value);
            //    if (soLuongNhap < _minEntry) {
            //        //dgvDanhSachNhap.CurrentCell = dgvDanhSachNhap.Rows[e.RowIndex].Cells["soLuongColumn"];
            //        e.Cancel = true;
            //        MessageBox.Show("Số lượng nhập tối thiểu là: " + _minEntry.ToString(), "Lỗi");
            //        return;
            //    }
            //    if (soLuongTon >= _minInStock) {
            //        MessageBox.Show("Số lượng tồn phải ít hơn: " + _minInStock.ToString(), "Lỗi");
            //        //dgvDanhSachNhap.CurrentCell = dgvDanhSachNhap.Rows[e.RowIndex].Cells["soLuongColumn"];
            //        e.Cancel = true;
            //        return;
            //    }
            //}
        }

        private void dgvDanhSachNhap_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) {            
            //kiem tra da co sach tren ton tai hay chua
           
            int tongSoLuongNhap  = 0;
            for (int i = 0; i < dgvDanhSachNhap.Rows.Count - 1; i++ ){             

                dgvDanhSachNhap.Rows[i].Cells["sttColumn"].Value = (i + 1).ToString();
                int soLuongNhap = Convert.ToInt32(dgvDanhSachNhap.Rows[i].Cells["soLuongColumn"].Value);
                tongSoLuongNhap += soLuongNhap;
            }
            tbxTongSoSach.Text = tongSoLuongNhap.ToString();
        }

        private void dgvDanhSachNhap_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e) {
            int tongSoLuongNhap = 0;
            for (int i = 0; i < dgvDanhSachNhap.Rows.Count; i++) {
                dgvDanhSachNhap.Rows[i].Cells["sttColumn"].Value = (i + 1).ToString();
                int soLuongNhap = Convert.ToInt32(dgvDanhSachNhap.Rows[i].Cells["soLuongColumn"].Value);
                tongSoLuongNhap += soLuongNhap;
            }
            tbxTongSoSach.Text = tongSoLuongNhap.ToString();
        }

        private void btnXoa_Click(object sender, EventArgs e) {
            dgvDanhSachNhap.Rows.Remove(dgvDanhSachNhap.CurrentRow);
        }
        
    }
}
